package com.uchiha.nova.mainService.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.uchiha.nova.core.model.entity.RoleEntity;
import com.uchiha.nova.core.model.entity.UserEntity;
import com.uchiha.nova.core.model.vo.UserInfoVO;
import com.uchiha.nova.core.model.vo.WebUserInfoVO;
import com.uchiha.nova.core.tool.support.NovaUser;
import com.uchiha.nova.mainService.service.IRoleService;
import com.uchiha.nova.mainService.service.UserService;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import com.uchiha.nova.mainService.mapper.UserMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表  服务实现类
 * </p>
 *
 * @author 周林静
 * @since 2021-04-30
 */
@Service
@Validated
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Resource
    IRoleService roleService;

    @Override
    public WebUserInfoVO getWebUserInfoVO(Long id) {
        UserEntity user = this.getById(id);
        List<RoleEntity> roleEntities = roleService.getUserRoles(id);
        String roles = "";
        List<String> roleList = new ArrayList<>();
        roleEntities.forEach(item->{
            roleList.add(item.getRoleName());
        });
        roles = StrUtil.join(",",roleList);
//        List<String> btnInfos = btnInfoService.getBtnByRoles(roleEntities);
        WebUserInfoVO webUserInfoVO = new WebUserInfoVO();
        UserInfoVO userInfo = new UserInfoVO();
        userInfo.setUsername(user.getUsername());
        userInfo.setIdCard(ObjectUtil.isNotEmpty(user.getIdentityCard())?user.getIdentityCard():"");
        userInfo.setAvatar(user.getAvatar());
        userInfo.setName(user.getName());
        userInfo.setTenantId(user.getTenantId());
        userInfo.setUserId(id.toString());
        webUserInfoVO.setUserInfo(userInfo);
        webUserInfoVO.setRoles(roles);
        webUserInfoVO.setPermission(new ArrayList<String>());
        return webUserInfoVO;
    }

    @Override
    public NovaUser getNovaUser(UserEntity userEntity) {
        NovaUser user = new NovaUser();
        user.setUserId(userEntity.getId());
        user.setUserName(userEntity.getUsername());
        user.setNickName(userEntity.getName());

        user.setDeptId(ObjectUtil.isEmpty(userEntity.getDeptId())?"1":String.valueOf(userEntity.getDeptId()));
        user.setCompanyId(ObjectUtil.isEmpty(userEntity.getCompanyId())?"1":String.valueOf(userEntity.getCompanyId()));
        user.setPostId(ObjectUtil.isEmpty(userEntity.getPostId())?"1":String.valueOf(userEntity.getPostId()));
        user.setTenantId(userEntity.getTenantId());
        List<RoleEntity> roleEntities = roleService.getUserRoles(userEntity.getId());
        String roles = "";
        List<String> roleList = new ArrayList<>();
        roleEntities.forEach(item->{
            roleList.add(item.getRoleName());
        });
        roles = StrUtil.join(",",roleList);
        user.setRoleName(roles);

        return user;
    }
}
